<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------

namespace app\api\validate\order;

use app\api\model\delivery\Company;
use app\core\base\BaseValidate;

/**
 * 寄售单验证器
 * Class Cart
 * @package app\api\validate\order
 */
class Order extends BaseValidate
{

    protected $rule = [
        'cart_ids' => 'require|array',
        'express_company_id' => 'require|gt:0|checkIsCompany',
        'express_number' => 'require',
        'take_address_id' => 'integer|gt:0'
    ];

    protected $message = [
        'cart_ids.require' => [ 'common_validate.require', [ 'cart_ids' ] ],
        'express_company_id.gt' => 'validate_order_create.express_company_id_require' ,
        'express_number.require' => 'validate_order_create.express_number_require',
        'take_address_id' => 'validate_order_create.take_address_id_right',
    ];

    protected $scene = [
        "create" => [ 'cart_ids', 'express_company_id', 'express_number','take_address_id'],
        "apply" => [ 'express_company_id', 'express_number','take_address_id'],
    ];

    protected function checkIsCompany($value, $rule, $data){
        $expressCompany = (new Company())->where('company_id',$value)->find();
        if(empty($expressCompany)){
            return "validate_order_create.express_company_id_right";
        }
        return true;
    }

}
